﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Thksoft.Module.DataDetail.Cotrols
{
    public partial class CtrlYearCapotal : UserControl
    {
        private QX_PLANYEARAPPLYBLL planYearApplyBLL = new QX_PLANYEARAPPLYBLL();
        private QS_PLANMONTH_DETAILBLL planMonthDetailBLL = new QS_PLANMONTH_DETAILBLL();
        private QS_PLANMONTHAPPLYBLL planMonthApplyBLL = new QS_PLANMONTHAPPLYBLL();

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="id">年资金申请ID</param>
        public void BindData(decimal id)
        {
            var entity = planYearApplyBLL.Find(id);
            if (entity == null)
            {
                Response.Write("<script>alert('【系统提示】该条数据有误，请与管理员联系');location.href='../Right.aspx';</script>");
                return;
            }
            lblTime.Text = entity.D_OPERATOR_TIME.ToString("yyyy年MM月dd日");
            lblUser.Text = new Sys_UserBLL().Find(entity.N_USER_ID).USER_NAME;
            lblyear.Text = entity.N_YEAR.ToString();
            lblBzyj.Text = entity.C_REAMRK == null ? "无" : entity.C_REAMRK;
            lbldw.Text = new Sys_CompanyBLL().Find(entity.N_COMPANY_ID).CMY_NAME;

            var data = planMonthApplyBLL.Where("N_YEARAPP_ID = " + entity.N_YEARAPP_ID, "N_MONTH DESC", 0, 1);
            QS_PLANMONTHAPPLY monthEntity = null;
            if (data.Count == 1)
            {
                monthEntity = data[0];
            }

            DataTable dt = new QX_PLANYEARAPPLYBLL().GetDataBind(entity.N_BUSINESS_ID.ToString());

            dt.Columns.Add("USE_MONEY");
            var detial = new List<QS_PLANMONTH_DETAIL>();
            if (monthEntity != null)
            {
                detial = planMonthDetailBLL.Where("N_MONTHAPP_ID = " + monthEntity.N_MONTHAPP_ID);
            }
            foreach (DataRow item in dt.Rows)
            {
                var proid = Convert.ToDecimal(item["N_PROID"]);
                var itm = detial.SingleOrDefault(a => a.N_PROID == proid);
                if (itm == null)
                {
                    item["USE_MONEY"] = null;
                }
                else
                {
                    decimal va = itm.N_USEAMOUNT + itm.N_AMOUNT;
                    if (va == 0)
                    {
                        item["USE_MONEY"] = null;
                    }
                    else
                    {
                        item["USE_MONEY"] = itm.N_USEAMOUNT + itm.N_AMOUNT;  // .ToString();
                    }
                }
            }
            gvMonthFunds.DataSource = dt;

            gvMonthFunds.DataBind();
        }

        #region 代码块

        protected void gvMonthFunds_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string type = ((Label)e.Row.FindControl("lbltype")).Text;
                if (type == "0")
                {
                    e.Row.Attributes["style"] = "background-color:#B2D8FF;font-size: 14px";

                    // e.Row.Cells[2].Attributes["style"] = "font-size: 14px";
                }
                else if (type == "1")
                {
                    e.Row.Attributes["style"] = "background-color:#CCCCCC;font-size: 13px;";

                    //  e.Row.Cells[2].Attributes["style"] = "font-size: 13px";
                }
            }
        }

        /// <summary>
        /// 打印事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void PrintClick(object sender, EventArgs e)
        {
            try
            {
                decimal id = Convert.ToDecimal(ViewState["busId"]);
                var entity = new QX_PLANYEARAPPLYBLL().FindByBuid(id);
                //var user = userBLL.GetInfoById(entity.N_USER_ID.ToString());
                Session["YEAREDITFUNDSHeader"] = entity.N_YEAR + "年计划申请";
                var sql = @"SELECT
	T1.N_YDETAIL_ID AS 详情编号,
	T1.N_PROID AS 项目编号,
	T1.C_PRONAME AS 项目名称,
	T1.N_AMOUNT AS 申请金额,
	DECODE(T1.N_TYPE,0,'主节点',1,'分节点',2,'子节点（可录入数据）','其它') AS 节点类型,
	T1.C_REMARK AS 备注
FROM QX_PLANYEAR_DETAIL T1
LEFT JOIN QS_BUSINESS T2
ON T1.N_BUSINESS_ID = T2.N_BUSINESS_ID
WHERE T1.N_BUSINESS_ID = {0}
ORDER BY T1.N_PROID
";
                sql = string.Format(sql, id);
                var dt = new QX_PLANYEARAPPLYBLL().Query(sql);
                Session["YEAREDITFUNDSBody"] = dt;
                MessageBox.ResponseScript(Page, "top.MessageBox.Print({Value:\"ID=BASIC&VALUE=YEAREDITFUNDS\",Area:[\"800px\",\"500px\"],Scrollbar:true})");
            }
            catch (Exception)
            {
                MessageBox.Show(Page, "打印异常");
            }
        }

        #endregion 代码块
    }
}